package com.borrowed.book.membercard.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.borrowed.book.book.constant.BookConstant;
import com.borrowed.book.coupon.service.SCouponService;
import com.borrowed.book.coupon.service.SCouponUserService;
import com.borrowed.book.membercard.entity.SMemberCard;
import com.borrowed.book.membercard.entity.SMembercardUser;
import com.borrowed.book.membercard.service.SMemberCardService;
import com.borrowed.book.membercard.mapper.SMemberCardMapper;
import com.borrowed.book.membercard.service.SMembercardUserService;
import com.borrowed.book.system.util.DayUtil;
import com.borrowed.book.user.domain.SUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
*
*/
@Service
public class SMemberCardServiceImpl extends ServiceImpl<SMemberCardMapper, SMemberCard>  implements SMemberCardService{
    @Autowired
    private SMembercardUserService membercardUserService;
    @Autowired
    private SCouponService couponService;

    /**
     * 添加会员卡
     *
     * @param memerCard
     */
    @Override
    public void addMemCard(SMemberCard memerCard) {
        this.save(memerCard);
    }

    /**
     * 更新会员卡
     *
     * @param memerCard
     */
    @Override
    public void updateMemCard(SMemberCard memerCard) {
        this.updateById(memerCard);
    }

    /**
     * 一览查询会员卡
     *
     * @return
     */
    @Override
    public List<SMemberCard> selectMemberCards() {
        List<SMemberCard> memCardList=this.list();
        return memCardList;
    }

    /**
     * 删除会员卡
     *
     * @param id
     */
    @Override
    public void deleteMemCard(Long id) {
        this.baseMapper.deleteById(id);
    }

    /**
     * 会员卡绑定
     * 给会员生成对应的优惠券
     * @param cardId 绑定卡的cardId
     */
    @Override
    public void bindingMemberCard(Long cardId,String openId) {
        SUser sysUser = (SUser) SecurityUtils.getSubject().getPrincipal();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        //微信回调获取不到openId,需要传入。
        if(openId==null||"".equals(openId)){
            openId=sysUser.getOpenid();
        }
        //会员开始日
        Date startDate=DayUtil.setStartHourMinite(calendar).getTime();
        //查询当前登录用户之前是否有会员记录
        List<SMembercardUser> recordList=membercardUserService.getMembercardUser(openId);
        if(recordList!=null&&recordList.size()>0) {
            SMembercardUser memcardUser = recordList.get(0);
            if (memcardUser != null) {
                    //若是会员未失效,新的结束日期在原来基础上累加
                    calendar.setTime(memcardUser.getEndTime());
                    calendar.add(Calendar.DAY_OF_YEAR,1);
                    startDate=DayUtil.setStartHourMinite(calendar).getTime();
            }
        }
        //计算会员到期日
        SMemberCard memCard=this.baseMapper.selectById(cardId);
        Date endTime=null;
        if(BookConstant.TIME_UNIT_WEEK.equals(memCard.getValidTimeUnit())){
            calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+7*memCard.getValidTime()); //给当前时间增加周
            calendar= DayUtil.setLastHourMinite(calendar);
            endTime = calendar.getTime();
        }else if(BookConstant.TIME_UNIT_MONTH.equals(memCard.getValidTimeUnit())){
            calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1*memCard.getValidTime()); //增加月
            calendar=DayUtil.setLastHourMinite(calendar);
            endTime = calendar.getTime();
            couponService.createMemberCardExpressCoupon(endTime,openId,memCard.getTranstportCouponsNum());
        }else if(BookConstant.TIME_UNIT_QUARTER.equals(memCard.getValidTimeUnit())){
            calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+3*memCard.getValidTime()); //增加季度
            calendar=DayUtil.setLastHourMinite(calendar);
            endTime = calendar.getTime();
            couponService.createMemberCardExpressCoupon(endTime,openId,memCard.getTranstportCouponsNum());
        }else if(BookConstant.TIME_UNIT_YEAR.equals(memCard.getValidTimeUnit())){
            calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)+1*memCard.getValidTime()); //增加年
            calendar=DayUtil.setLastHourMinite(calendar);
            endTime=calendar.getTime();
            couponService.createMemberCardExpressCoupon(endTime,openId,memCard.getTranstportCouponsNum());
        }
        //新增绑定会员信息
        SMembercardUser memcardUser=new SMembercardUser();
        memcardUser.setStartTime(startDate);
        memcardUser.setCreateTime(new Date());
        memcardUser.setCardId(cardId);
        memcardUser.setOpenId(openId);
        memcardUser.setEndTime(endTime);
        memcardUser.setBorrowTimes(memCard.getBorrowTimes());
        memcardUser.setStatus(BookConstant.MEMCARD_STATUS_VALID);
        membercardUserService.getBaseMapper().insert(memcardUser);
    }


}
